
#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height
#import "CollectionViewAlbum.h"
#import "CommendViewController.h"
#import "WebViewOfCommendViewController.h"
@interface CollectionViewAlbum ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) UILabel *label;
@end
@implementation CollectionViewAlbum
#pragma mark - storyboard或xib 初始化
#if 0
// 先走的初始化方法, 没有的话在走别的初始化方法
- (instancetype)initWithCoder:(NSCoder *)coder{
    self = [super initWithCoder:coder];
    if (self) {
        
        NSLog(@"%lf",self.frame.size.width);
        [self setUp];
        self.arrOfImages = [NSMutableArray array];
        [self startTimer];
    }
    return self;
}
- (void)setUp{
    [self createCollectionView];
    
    
}

#endif
#pragma mark - 代码初始化
#if 1
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.userInteractionEnabled = NO;
        self.arrOfImages = [NSMutableArray array];
        self.arrOfWebView = [NSMutableArray array];
        self.arrOfLabelText = [NSMutableArray array];
            [self createCollectionView];
            
            [self createPageControl];
            
        
        
        
        
    }
    return self;
}
#endif


#pragma mark - 共同方法
- (void)createCollectionView{
    self.layout = [[UICollectionViewFlowLayout alloc]init];
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:_layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self addSubview:self.collectionView];
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _layout.sectionInset = UIEdgeInsetsZero;
    _layout.minimumLineSpacing = 0;
    _layout.minimumInteritemSpacing = 0;
    _collectionView.pagingEnabled = YES;
    [_collectionView registerClass:[Cell class] forCellWithReuseIdentifier:@"CollectionViewAlbum"];
    
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 100;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
     _pageControl.numberOfPages = _arrOfImages.count;
    return _arrOfImages.count ;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewAlbum" forIndexPath:indexPath];
    
    // cell.cycleImage.image = [UIImage imageNamed:_arrOfImages[indexPath.row]];
    cell.backgroundColor = [UIColor redColor];
    [cell.cycleImage sd_setImageWithURL:[NSURL URLWithString:_arrOfImages[indexPath.row]]];
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.delegate respondsToSelector:@selector(gogogo:)]) {
        [self.delegate gogogo:indexPath];
    }
    CommendViewController *vc = (CommendViewController *)self.nextResponder.nextResponder.nextResponder;
    WebViewOfCommendViewController *headVC = [[WebViewOfCommendViewController alloc]init];
    headVC.url = self.arrOfWebView[indexPath.row];
    [vc.navigationController pushViewController:headVC animated:YES];
}



- (void)setArrOfImages:(NSMutableArray *)arrOfImages{
    _arrOfImages = arrOfImages;
    [_collectionView reloadData];
    if (self.arrOfImages.count>0) {
       
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:50] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
        
        [self startTimer];
    }    
}
- (void)setArrOfWebView:(NSMutableArray *)arrOfWebView{
    _arrOfWebView = arrOfWebView;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    
    _collectionView.frame = self.bounds;
    _layout.itemSize = self.bounds.size;
        [_collectionView setContentOffset:CGPointMake(self.bounds.size.width, 0)];
    _pageControl.frame =CGRectMake((self.frame.size.width - 100)/2, self.frame.size.height-50, 100, 50);
    
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, self.frame.size.height-50, self.frame.size.width, 50)];
    [self addSubview:self.label];
    _label.backgroundColor = [UIColor clearColor];
    
    
    
    
    
    
}

- (void)createPageControl{
    
    self.pageControl = [[UIPageControl alloc]init];

    [self addSubview:self.pageControl];
    
    _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    [_pageControl addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)pageControlAction:(UIPageControl *)pageControl{
    
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:pageControl.currentPage inSection:50] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
}
#pragma mark - 计时器
- (void)startTimer{
    
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(handleNext) userInfo:nil repeats:YES];
    
    
    
}

- (void)handleNext{

    NSIndexPath *currentIndexPath = [[self.collectionView indexPathsForVisibleItems] lastObject];
    
    NSIndexPath *number50 = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:50];
    
    [self.collectionView scrollToItemAtIndexPath:number50 atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    // 下一个item
    NSInteger nextItem = number50.item + 1;
    // 第50个区
    NSInteger section = number50.section;
    
    if (nextItem == self.arrOfImages.count ) {
        // 到50区的最后一个item时,区+1
        nextItem = 0;
        section++;
    }
    
    
    NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:section];
    
    
    
    [self.collectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    
}
#pragma mark - pageControll
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (self.arrOfImages.count>0) {
        int page = (int)(scrollView.contentOffset.x / WIDTH + 0.5) % self.arrOfImages.count;
        
        self.pageControl.currentPage = page;

    }
    
}

@end


#pragma mark - 创建cell类
@implementation Cell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _cycleImage = [[UIImageView alloc]initWithFrame:self.bounds];
        [self.contentView addSubview:_cycleImage];
    }
    return self;
}
@end
